﻿using pdfer.FileObjects;
using pdfer.Syntax;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pdfer.PdfObjecs.Fonts
{
    internal class FontCIDDictionary : PdfFileObject, IFontTypeDictionary
    {
        public FontCIDDictionary(PdfFileObject pdfFileObject)
            : base(pdfFileObject)
        {
            if (pdfFileObject.TryGetValue(PdfName.Key_Type, out object? val))
            {
                Type = PdfParserUtils.ParseString(val);
                if (Type != PdfTypeConst.Type_Font)
                {
                    throw new Exception("类型必须是 /Font");
                }
            }
            if (pdfFileObject.TryGetValue(PdfName.Key_Subtype, out val))
            {
                Subtype = PdfParserUtils.ParseString(val);
                if (Subtype != PdfTypeConst.Type_Font_CIDFont0
                    && Subtype != PdfTypeConst.Type_Font_CIDFont2)
                {
                    throw new Exception("类型必须是 /CIDFontType0 /CIDFontType2");
                }
            }
        }
        public string Type { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Subtype { get; set; }

        public object Encoding => throw new NotSupportedException();

        public PdfFileObject? ToUnicode => throw new NotSupportedException();

        public string BaseFont
        {
            get
            {
                return GetDicStringVal(PdfName.Key_BaseFont);
            }
        }

        public CIDSystemInfoDictionary CIDSystemInfo
        {
            get
            {
                if(TryGetValue(PdfName.Key_CIDSystemInfo, out var obj))
                {
                    if(obj is String id)
                    {
                        return GetIndirectObj<CIDSystemInfoDictionary>(id);
                    }
                }
                return null;
            }
        }

        /// <summary>
        /// （在加标签的 PDF 文档中是必需的;必须是间接引用）描述字体的默认度量（而不是其字形宽度）的字体描述符（参见第 5.7 节 “字体描述符”）。
        /// </summary>
        public FontDescriptorDictionary FontDescriptor
        {
            get
            {
                var obj = GetDicPdfFileObjVal<FontDescriptorDictionary>(PdfName.Key_ToUnicode);
                return obj;
            }
        }

        /// <summary>
        /// CIDFont 中字形的默认宽度（请参阅第 439 页上的“CIDFonts 中的字形度量”）。默认值：1000。
        /// </summary>
        public Int32 DW { get { return GetDicInt32Val(PdfName.Key_DW, 1000); } }
        /// <summary>
        /// 
        /// </summary>
        public List<Object> W { get { return GetDicListVal(PdfName.Key_W); } }
        public List<Object> DW2 { get { return GetDicListVal(PdfName.Key_DW2); } }
        public List<Object> W2 { get { return GetDicListVal(PdfName.Key_W2); } }

        public Object CIDToGIDMap
        {
            get
            {
                if(Subtype != PdfTypeConst.Type_Font_CIDFont2)
                {
                    return null;
                }
                throw new NotImplementedException();
            }
        }

        public string GetString(ReadOnlySpan<Byte> data)
        {
            throw new NotImplementedException();
        }
    }
}
